import request from '@/utils/request'

// 获取问卷列表
export function getQuestionnaireList(params) {
  return request({
    url: '/questionnaire/list',
    method: 'get',
    params
  })
}

// 获取问卷详情
export function getQuestionnaireDetail(id) {
  return request({
    url: `/questionnaire/${id}`,
    method: 'get'
  })
}

// 获取问卷题目
export function getQuestionList(questionnaireId) {
  return request({
    url: '/questionnaire/question/list',
    method: 'get',
    params: { questionnaireId }
  })
}

// 获取题目选项
export function getOptionList(questionId) {
  return request({
    url: '/questionnaire/option/list',
    method: 'get',
    params: { questionId }
  })
}

// 提交答卷
export function submitAnswer(data) {
  return request({
    url: '/questionnaire/answer/submit',
    method: 'post',
    data
  })
}

// 提交答卷（支持答案详情）
export function submitAnswerWithDetails(data) {
  return request({
    url: '/questionnaire/answer/submitWithDetails',
    method: 'post',
    data
  })
}

// 获取问卷统计
export function getQuestionnaireStats(id) {
  return request({
    url: `/questionnaire/stats/${id}`,
    method: 'get'
  })
}

// ========== PC端管理API ==========

// 问卷管理
export function listQuestionnaire(params) {
  return request({
    url: '/questionnaire/list',
    method: 'get',
    params
  })
}

export function getQuestionnaire(id) {
  return request({
    url: `/questionnaire/${id}`,
    method: 'get'
  })
}

export function addQuestionnaire(data) {
  return request({
    url: '/questionnaire',
    method: 'post',
    data
  })
}

export function updateQuestionnaire(data) {
  return request({
    url: '/questionnaire',
    method: 'put',
    data
  })
}

export function delQuestionnaire(ids) {
  return request({
    url: `/questionnaire/${ids}`,
    method: 'delete'
  })
}

export function publishQuestionnaire(id) {
  return request({
    url: `/questionnaire/publish/${id}`,
    method: 'put'
  })
}

export function closeQuestionnaire(id) {
  return request({
    url: `/questionnaire/close/${id}`,
    method: 'put'
  })
}

// 题目管理
export function listQuestion(params) {
  return request({
    url: '/questionnaire/question/list',
    method: 'get',
    params
  })
}

export function getQuestion(id) {
  return request({
    url: `/questionnaire/question/${id}`,
    method: 'get'
  })
}

export function addQuestion(data) {
  return request({
    url: '/questionnaire/question',
    method: 'post',
    data
  })
}

export function updateQuestion(data) {
  return request({
    url: '/questionnaire/question',
    method: 'put',
    data
  })
}

export function delQuestion(ids) {
  return request({
    url: '/questionnaire/question/batch',
    method: 'delete',
    data: ids
  })
}

// 答卷管理
export function listAnswer(params) {
  return request({
    url: '/questionnaire/answer/list',
    method: 'get',
    params
  })
}

export function getAnswer(id) {
  return request({
    url: `/questionnaire/answer/${id}`,
    method: 'get'
  })
}

export function delAnswer(ids) {
  return request({
    url: `/questionnaire/answer/${ids}`,
    method: 'delete'
  })
}

// 导出答卷
export function exportAnswer(params) {
  return request({
    url: '/questionnaire/answer/export',
    method: 'post',
    data: params,
    responseType: 'blob'
  })
}

// 选项管理API
export function listOptions(questionId) {
  return request({
    url: '/questionnaire/option/list',
    method: 'get',
    params: { questionId }
  })
}

export function addOption(data) {
  return request({
    url: '/questionnaire/option',
    method: 'post',
    data
  })
}

export function updateOption(id, data) {
  return request({
    url: `/questionnaire/option/${id}`,
    method: 'put',
    data
  })
}

export function delOption(id) {
  return request({
    url: `/questionnaire/option/${id}`,
    method: 'delete'
  })
}

export function delOptionsByQuestion(questionId) {
  return request({
    url: `/questionnaire/option/byQuestion/${questionId}`,
    method: 'delete'
  })
}

// ========== 问卷设计工具API ==========

// 获取问卷设计数据
export function getDesignData(id) {
  return request({
    url: `/questionnaire/designer/detail/${id}`,
    method: 'get'
  })
}

// 获取草稿问卷列表
export function getDraftQuestionnaires() {
  return request({
    url: '/questionnaire/designer/drafts',
    method: 'get'
  })
}

// 保存问卷设计
export function saveDesign(id, data) {
  return request({
    url: `/questionnaire/designer/save/${id}`,
    method: 'post',
    data
  })
}

// 预览问卷
export function previewQuestionnaire(id) {
  return request({
    url: `/questionnaire/designer/preview/${id}`,
    method: 'get'
  })
}

// 获取设计模板列表
export function getDesignTemplates() {
  return request({
    url: '/questionnaire/designer/templates',
    method: 'get'
  })
}

// 保存问卷设计
export function saveQuestionnaireDesign(data) {
  return request({
    url: '/questionnaire/designer/save',
    method: 'post',
    data
  })
} 